package com.egao.orderB.service.impl;

import com.egao.common.core.constants.UserConstant;
import com.egao.common.core.utils.Utils;
import com.egao.common.core.utils.messages.PhoneUtils;
import com.egao.common.core.web.PageResult2;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.biz.UserBiz;
import com.egao.common.system.entity.User;
//import com.egao.orderB.mapper.DriverMapper;
import com.egao.common.system.service.UserService;
import com.egao.orderB.entity.Customer;
import com.egao.orderB.entity.Driver;
import com.egao.orderB.service.DriverService;
import org.springframework.stereotype.Service;
import com.egao.orderB.biz.DriverBiz;
import net.sf.json.JSONObject;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
//import javax.transaction.Transactional;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 司机管理服务实现类
 * @author : Doman
 * @date : 2020-06-03 16:58:06
 * @version ：1.0
 */
@Service
public class DriverServiceImpl /*extends ServiceImpl<DriverMapper, Driver> */implements DriverService {

	@Autowired
	private DriverBiz driverBiz;
	@Autowired
	private UserService userService;
	@Autowired
	private UserBiz userBiz;

    /**
    @Override
    public PageResult<Driver> listPage(PageParam<Driver> page) {
        List<Driver> records = baseMapper.listPage(page);
        return new PageResult<>(records, page.getTotal());
    }

    @Override
    public List<Driver> listAll(Map<String, Object> page) {
        return baseMapper.listAll(page);
    } */

    @Override
    public JSONObject list(PageResult2<T> page, Driver driver, User loginUser)  throws Exception{
        return driverBiz.list(page,driver,loginUser);
    }

    @Override
    public List<Driver> list(Driver driver, User loginUser) {
        return driverBiz.list(driver,loginUser);
    }

    @Override
    public Driver getById(Driver driver) {
        return driverBiz.getById(driver);
    }

    @Transactional
    @Override
    public JsonResult save(Driver driver, User loginUser) {
        if (driverBiz.save(driver, loginUser)) {
            return JsonResult.ok("添加成功");
        }
        return JsonResult.error("添加失败");
    }

    @Transactional
    @Override
    public JsonResult platsave(Driver driver,User user, User loginUser) {
        driver.setCompanyId(UserConstant.COMPANY_ID + "");
        driver.setState("已审核");
        driver.setPhone(user.getUsername());
        int key= driverBiz.apiSave(driver, loginUser);
        user.setCompanyId(UserConstant.COMPANY_ID);
        user.setCustomerId(key);
        user.setType("司机");
        user.setAdmin("0");
        user.setState(0);
        user.setPassword(userService.encodePsw(UserConstant.DEFAULT_PSW));
        key= userService.apiSave(user);
        user = new User();
        user.setUserId(key);
        user.setRoleId(UserConstant.DRIVER_ROLE_ID);
        userService.saveUserRole(user);
        return JsonResult.ok("注册成功!默认密码为123123");
    }
//
//    @Transactional
//    @Override
//    public JsonResult apiSaveUserFirst(Driver driver, User loginUser, User user) {
//        driver.setCompanyId(UserConstant.COMPANY_ID+"");
//        driver.setState("未审核");
//        int key= driverBiz.apiSave(driver, loginUser);
//        user.setCustomerId(key);
//        userService.apiSetCustomerId(user, loginUser);
//        return JsonResult.ok("添加成功");
//    }

    @Transactional
    @Override
    public JsonResult apiSave(Driver driver, User loginUser, User user) {
        if(Utils.isEmpty(driver.getName()) || Utils.isEmpty(driver.getCarNo())
                || Utils.isEmpty(driver.getIdCard())|| Utils.isEmpty(user.getUsername())
                || Utils.isEmpty(user.getPassword())
        ){
            return JsonResult.error("请填写司机信息,操作失败!");
        }
        driver.setCompanyId(UserConstant.COMPANY_ID + "");
        driver.setState("未审核");
        driver.setPhone(user.getUsername());
        int key= driverBiz.apiSave(driver, loginUser);
        user.setCompanyId(UserConstant.COMPANY_ID);
        user.setCustomerId(key);
        user.setType("司机");
        user.setAdmin("0");
        user.setState(2);
        if (Utils.isEmpty(user.getPhone())) {
            user.setPhone(user.getUsername());
        }
        user.setPassword(userService.encodePsw(user.getPassword()));
        key= userService.apiSave(user);
        user = new User();
        user.setUserId(key);
        user.setRoleId(UserConstant.DRIVER_ROLE_ID);
        userService.saveUserRole(user);
        return JsonResult.ok("注册成功");
    }

    @Transactional
    @Override
    public JsonResult update(Driver driver, User loginUser) {
        if (driverBiz.update(driver, loginUser)) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }
    @Transactional
    @Override
    public JsonResult driverUpdate(Driver driver, User loginUser) {
        if (driverBiz.driverUpdate(driver, loginUser)) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }
    @Transactional
    @Override
    public JsonResult platUpdate(Driver driver, User loginUser) {
        if (driverBiz.platUpdate(driver, loginUser)) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    @Transactional
    @Override
    public JsonResult remove(Driver driver, User loginUser) {
        if (driverBiz.remove(driver, loginUser)) {
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }

    @Transactional
    @Override
    public JsonResult examine(Driver driver, User loginUser) {
        driver.setState("已审核");
        if (driverBiz.examine(driver, loginUser)) {
            User u2 = userService.apiGetByCustomerId(driver.getId(), "司机");
            u2.setState(0);
            userBiz.apiExamineCustomerId(u2, loginUser);

            Driver driver2 = driverBiz.getById(driver);
            PhoneUtils.sendMsg(driver2.getPhone(), "600424", new String[]{u2.getUsername()});
            return JsonResult.ok("审核通过!");
        }
        return JsonResult.error("审核未通过");
    }

}
